package com.example.springboot_sdse_air.controller;

import com.example.springboot_sdse_air.common.Result;
import com.example.springboot_sdse_air.service.DeepSeekService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能：
 * 作者：王彬 202200111149
 * 日期：2025-04-29 9:26
 */

@CrossOrigin
@RestController
@RequestMapping("/api/deepseek")
@RequiredArgsConstructor
public class DeepSeekController {

    @Resource
    private final DeepSeekService deepSeekService;

    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> streamResponse(@RequestParam String prompt) {
        return deepSeekService.streamResponse(prompt)
                .concatWith(Mono.just("[DONE]"));  // 最后手动发送一个结束标志;
    }

//    @GetMapping(value = "/response", produces = MediaType.APPLICATION_JSON_VALUE)
//    public Mono<Result> getDeepSeekResponse(@RequestParam String prompt) {
//        long start = System.currentTimeMillis();
//        return deepSeekService.deepSeekResponse(prompt)
//                .map(content -> {
//                    long end = System.currentTimeMillis();
//                    System.out.println("整条请求耗时：" + (end - start) + " ms");
//                    return Result.success(content);
//                });
////                .map(Result::success);  // 返回完整文本
//    }


    //    @GetMapping("/test")
//    public Mono<String> testDeepSeek(@RequestParam String prompt) {
//        System.out.println("开始调用deepSeekService.testResponse(prompt)");
////        Mono<String> msg =
////        String result = deepSeekService.testResponse(prompt).block(Duration.ofSeconds(60)); // 阻塞最多等60秒
////        System.out.println("收到响应内容：" + result);
//        System.out.println("结束调用deepSeekService.testResponse(prompt)");
//        return deepSeekService.testResponse(prompt);
//    }
}
